
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Windows;
using System.Windows.Input;
using AdventureWorks.Client.Objects;
using AdventureWorks.Services;
using Xomega.Framework;

namespace AdventureWorks.Client.WPF
{
    public partial class EmployeeSearch : Window
    {
        private EmployeeCriteria criteria;
        private EmployeeRowObject list;

        public EmployeeSearch()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            criteria = new EmployeeCriteria();
            pnlCriteria.DataContext = criteria;
            list = new EmployeeRowObject();
            list.Editable = false;
            gridResults.ItemsSource = list;
            PostInitialize();
        }

        partial void PostInitialize();

        public void Refresh()
        {
            btnSearch_Click(null, null);
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            criteria.Validate(true);
            ErrorList valErr = criteria.GetValidationErrors();
            if (valErr.HasErrors())
            {
                Errors.Show(valErr);
                return;
            }
            try
            {
                IEmployeeService svcEmployee = new AdventureWorks.Services.EmployeeService();
                Employee_ReadListInput inReadList = new Employee_ReadListInput();
                criteria.ToDataContract(inReadList);
                IEnumerable<Employee_ReadListOutput> outReadList = svcEmployee.ReadList(inReadList);
                list.FromDataContract(outReadList);
                if (svcEmployee is IDisposable) ((IDisposable)svcEmployee).Dispose();
            }
            catch (Exception ex)
            {
                Errors.Show(ErrorList.FromException(ex));
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            list.Clear();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
